package com.gitee.zycra.union.api.web.controller;

import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.param.AddProjectParam;
import com.gitee.zycra.union.api.common.param.UpdateProjectParam;
import com.gitee.zycra.union.api.common.vo.ProjectDetailVO;
import com.gitee.zycra.union.api.common.vo.ProjectVO;
import com.gitee.zycra.union.api.service.ApiProjectService;
import com.gitee.zycra.union.api.web.annotation.AuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@RestController
@RequestMapping("/{groupId}/project")
public class ApiProjectController {

    @Autowired
    private ApiProjectService apiProjectService;

    /**
     * 添加项目
     *
     * @param groupId         项目组ID
     * @param addProjectParam 项目
     * @return 添加结果
     */
    @AuthCheck({"3"})
    @PostMapping("/addProject")
    public CommonResult<String> addProject(@PathVariable("groupId") Long groupId, @RequestBody @Validated AddProjectParam addProjectParam) {
        addProjectParam.setGroupId(groupId);
        return apiProjectService.addProject(addProjectParam);
    }

    /**
     * 根据ID更新项目
     *
     * @param groupId            项目组ID
     * @param updateProjectParam 项目
     * @return 添加结果
     */
    @AuthCheck({"3"})
    @PostMapping("/updateProject")
    public CommonResult<String> updateProject(@PathVariable("groupId") Long groupId, @RequestBody @Validated UpdateProjectParam updateProjectParam) {
        updateProjectParam.setGroupId(groupId);
        return apiProjectService.updateProject(updateProjectParam);
    }

    /**
     * 根据ID删除项目
     *
     * @param groupId   项目组ID
     * @param projectId 项目ID
     * @return 添加结果
     */
    @AuthCheck({"3"})
    @PostMapping("/deleteProject")
    public CommonResult<String> deleteProject(@PathVariable("groupId") Long groupId, @RequestParam("projectId") Long projectId) {
        return apiProjectService.deleteProject(groupId, projectId);
    }

    /**
     * 根据项目组ID查询项目
     *
     * @param groupId 项目组ID
     * @return 项目集合
     */
    @AuthCheck
    @PostMapping("/projectList")
    public CommonResult<List<ProjectVO>> projectList(@PathVariable("groupId") Long groupId) {
        return apiProjectService.projectList(groupId);
    }

    /**
     * 根据项目ID查询项目
     *
     * @param groupId   项目组ID
     * @param projectId 项目ID
     * @return 项目
     */
    @AuthCheck
    @PostMapping("/projectDetail")
    public CommonResult<ProjectDetailVO> projectDetail(@PathVariable("groupId") Long groupId, @RequestParam("projectId") Long projectId) {
        return apiProjectService.projectDetail(groupId, projectId);
    }
}
